import {createApp, h} from 'vue'
import App from './App.vue'
import axios from     "axios"
import element, {ElMessage, ElNotification} from 'element-plus'
import 'element-plus/dist/index.css'
import router  from "@/utils/router.js";
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)
//使用图标管理包
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
//基础配置axios
axios.defaults.baseURL="http://localhost:8080"
// //请求拦截
axios.interceptors.request.use(config => {
   // 请求拦截器
    if (localStorage.getItem("token")!=null) {
        // 每一个请求头携带token
        console.log("前端请求拦截加头"+localStorage.getItem("token"))
        config.headers.Authorization = localStorage.getItem("token");

    }
    return config
}, error => Promise.reject(error));
//响应拦截
axios.interceptors.response.use(result=>{
         if (result.data.code==0)
         {
             //成功就放行
             return result
         }
         if (result.data.code==1){
             return   ElNotification({
                 title: '消息',
                 message: h('i', { style: 'color: teal' },"操作失败!"),
             })

         }


},   err =>{
    if (err.response.status === 401) {
    ElMessage.error('主人，请先登录吧')
    router.push('/login')
} else {
    ElMessage.error('服务异常')
}
return Promise.reject(err);//异步的状态转化成失败的状态
 } )
app.provide("axios",axios)
app.use(element)
app.use(router)
app.mount('#app')